package com.example.campustalent.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.CompoundButton
import com.example.campustalent.MainActivity
import com.example.campustalent.bean.UserInfoBean
import com.example.campustalent.databinding.ActivityLoginBinding
import com.example.campustalent.utils.DbUtils
import com.example.campustalent.utils.SharedPreferencesUtils
import com.luck.picture.lib.tools.ToastUtils

class LoginActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLoginBinding
    private lateinit var preferencesUtils: SharedPreferencesUtils
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        val rootView: View = binding.root
        setContentView(rootView)
        if (DbUtils.liteOrm == null) {
            DbUtils.createDb(this, "CampusTalent")
        }
        preferencesUtils = SharedPreferencesUtils(this)
        if (preferencesUtils.getParam("name", "").toString()
                .isNotBlank() && preferencesUtils.getParam("password", "").toString().isNotBlank()
        ){
            binding.etName.setText(preferencesUtils.getParam("name", "").toString())
            binding.etPwd.setText(preferencesUtils.getParam("password", "").toString())
        }
            binding.etName.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(
                    s: CharSequence,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    if (!TextUtils.isEmpty(s.toString())) {
                        binding.ivDelete.visibility = View.VISIBLE
                    } else {
                        binding.ivDelete.visibility = View.GONE
                    }
                }
            })

        binding.etPwd.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s.toString())) {
                    binding.cbPwd.visibility = View.VISIBLE
                } else {
                    binding.cbPwd.visibility = View.GONE
                }
            }
        })
        binding.cbPwd.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            binding.cbPwd.isChecked = isChecked
            if (isChecked) {
                binding.cbPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            } else {
                binding.cbPwd.transformationMethod = PasswordTransformationMethod.getInstance()
            }
            binding.etPwd.setSelection(binding.etPwd.text.toString().length)
        })
        binding.tvRegister.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }
        binding.ivDelete.setOnClickListener {
            binding.etName.setText("")
        }
        binding.tvLogin.setOnClickListener {
            if (binding.etName.text.toString().isEmpty() || binding.etPwd.text.toString()
                    .isEmpty()
            ) {
                ToastUtils.s(this, "用户名或密码不能为空")
                return@setOnClickListener
            }
            val list = DbUtils.getQueryAll(UserInfoBean::class.java)
            if (!list.isNullOrEmpty()) {
                for (i in list.indices) {
                    if (binding.etName.text.toString() == list[i].name ) {
                        preferencesUtils.setParam("name", binding.etName.text.toString())
                        preferencesUtils.setParam("password", binding.etPwd.text.toString())
                        ToastUtils.s(this, "登陆成功")
                        startActivity(
                            Intent(this, MainActivity::class.java).putExtra(
                                "name",
                                binding.etName.text.toString()
                            )
                        )

                    }else if (binding.etName.text.toString() != list[i].name && i == list.size - 1){
                        ToastUtils.s(this, "用户不存在或密码错误")
                        return@setOnClickListener
                    }
                }
            } else {
                ToastUtils.s(this, "用户不存在或密码错误")
                return@setOnClickListener
            }
            preferencesUtils.setParam("name", binding.etName.text.toString())
            preferencesUtils.setParam("password", binding.etPwd.text.toString())
            ToastUtils.s(this, "登陆成功")
            startActivity(
                Intent(this, MainActivity::class.java).putExtra(
                    "name",
                    binding.etName.text.toString()
                )
            )

        }
    }
}